********************************************************************************
********************************************************************************
* Title: 		stacked
* Project:		Stress, Ethnicity, and Prosocial Behavior
* Author:		Moritz Poll (moritz.poll@brown.edu)
* PIs:			Johannes Haushofer, Sara Lowes, Abednego Musau, David Ndetei, 
*				Nathan Nunn, Moritz Poll, Nancy Qian
* Purpose:		Analysis when stacking game data
********************************************************************************
********************************************************************************

use  "$cleandata_dir/HIO_for_analysis_xlong.dta", clear

eststo clear
local x "eth"
local game "dg"
gen weights = 1*(game_str == "dg" | game_str == "tg1") + .2*(game_str == "tg2") + 1.5*(game_str == "social_proximity") + 0.5*(game_str == "trust" | game_str == "likely_to_be_friends" | game_str == "closeness")

forvalues sp = 1/3 {
	forvalues aweight = 0/1 {
		if `aweight' == 1 local weights [aweight=weights]
		else local weights ""
		if `sp' == 1 eststo: reg share treatment##_same_eth 1._same_sex##treatment  1._same_age##treatment i.game_extended  $controls `weights', cluster(ID)
		if `sp' == 2 {
		replace game_extended = 6 if game_str == "social_proximity"
		eststo: reg share treatment##_same_eth 1._same_sex##treatment  1._same_age##treatment i.game_extended  $controls `weights', cluster(ID)
		}
		if `sp' == 3 {
		replace game_extended = 6 if game_str == "trust" | game_str == "likely_to_be_friends" | game_str == "closeness"
		replace game_extended = . if game_str == "social_proximity"
		eststo: reg share treatment##_same_eth 1._same_sex##treatment  1._same_age##treatment i.game_extended  $controls `weights', cluster(ID)
		}

		lincom _b[1._same_`x'] + `share_hydro'*_b[1._same_`x'#1.treatment]
		pstar, b(`r(estimate)') se(`r(se)') p(`=2*ttail(r(df),abs(r(estimate)/r(se)))') precision($precision)
		estadd local diff_eth	  = r(bstar)
		estadd local diff_eth_SE  = r(sestar)
		lincom _b[1.treatment] + `share_same_`x''*_b[1._same_`x'#1.treatment] + ///
								 `share_same_sex'*_b[1._same_sex#1.treatment] + ///
								 `share_same_age'*_b[1._same_age#1.treatment]
		pstar, b(`r(estimate)') se(`r(se)') p(`=2*ttail(r(df),abs(r(estimate)/r(se)))') precision($precision)
		estadd local pill	  = r(bstar)
		estadd local pill_SE  = r(sestar)						 
		lincom _b[1._same_`x'#1.treatment]
		pstar, b(`r(estimate)') se(`r(se)') p(`=2*ttail(r(df),abs(r(estimate)/r(se)))') precision($precision)
		estadd local eth_Delta   = r(bstar)
		estadd local eth_DeltaSE = r(sestar)

		estadd local clust = "{" + string(e(N_clust)		,"%6.0fc") + "}" // The {} serve to avoid the scientific alignment
		estadd local obs   = "{" + string(e(N)				,"%6.0fc") + "}"
		estadd local dec   = "{" + string(e(N)/e(N_clust)	,"%6.0fc") + "}"
		
		sum share `weights' if e(sample)
		estadd local mean = string(r(mean),"%9.`=$precision'f")
		estadd local sd = "(" + string(r(sd),"%9.`=$precision'f") + ")"
		
		if `aweight' estadd local weights "Yes"
		else estadd local weights "No"
		if `sp' == 1 estadd local sp "No"
		if `sp' == 2 estadd local sp "Index"
		if `sp' == 3 estadd local sp "Full"
	}
}

esttab using "$tables_dir/stacked.tex", $table_design drop(*) nomtitles ///
	stats(mean sd pill pill_SE  diff_eth diff_eth_SE  eth_Delta eth_DeltaSE  weights sp clust dec obs, labels("Sample mean" "Sample standard deviation" "\midrule\multicolumn{4}{l}{\textit{Panel A: Average hydrocortisone effect$^a$}} \\ \quad Hydrocortisone effect" " " 	"\midrule \multicolumn{4}{l}{\textit{Panel B: Average Coethnicity effect$^b$}} \\ \quad Coethnicity effect" " " "\midrule\multicolumn{4}{l}{\textit{Panel C: Interaction of hydrocortisone and coethnicity$^c$}} \\ \quad Interaction effect" " " "\midrule Weights" "Social Proximity" "\midrule Participants" "Decisions per participant" "Decisions")) ///
	nonotes addnotes(`notesspec1' This table reports the results of stacking the data of the dictator and trust game (columns (1) and (2)), as well as the social proximity survey index (columns (3) and (4)) or the social proximity survey questions separately (columns (5) and (6)) respectively. Odd-numbered columns use the data as is while even-numbered columns weight all games to have equal influence despite trust game stage 2 having 30 observations per participant, while the dictator game and trust game stage 1 have 6 observations per participant, the social proximity index 4 and the individual questions 12. `general_notes') ///
	substitute(\_ _ "{l}{\footnotesize" "{p{\hsize}}{\footnotesize")
	
